package com.jushu.storbox.fragment;

import java.util.List;
import java.util.Map;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;

import com.jushu.storbox.R;
import com.jushu.storbox.ShareDriveMainFragmentActivity;
import com.jushu.storbox.adapter.UploadListAdapter;
import com.jushu.storbox.entities.FileInfo;
import com.jushu.storbox.service.ICloudDriveService;
import com.jushu.storbox.util.LogUtil;
import com.jushu.storbox.util.PublicTools;
import com.jushu.storbox.view.pulltorefresh.MyScrollView;

@SuppressLint("ValidFragment") public class UploadListFragment extends BaseFragment {
	
	private final static String LOG_TAG = "UploadListFragment";
	
	
	private View viewLayoutInflater;
	
	private List<FileInfo> list;
	
	/** cloud drive service */
	private ICloudDriveService shareDriveService;
	
	/** activity object */
	private Activity activity;
	
	private Handler activityHandler;
	
	private MyScrollView upload_list_scrollview;
	
	private LinearLayout upload_list_layout; 
	
	public UploadListFragment() {
		// TODO Auto-generated constructor stub
	}
	
	public UploadListFragment(Context context){
		

	}
	
	@Override
	public void onAttach(Activity activity) {
		// TODO Auto-generated method stub
		setBack(false);
		this.activity = (ShareDriveMainFragmentActivity) activity;
		if (null == getObj()) {
			// System.exit(1);
			PublicTools.restartApp(activity);
		} else {
			Map<String, Object> mp = (Map<String, Object>) getObj();
			if (null != (Handler) mp.get("activityhandler")) {
				activityHandler = (Handler) mp.get("activityhandler");
			} else {
				PublicTools.restartApp(activity);
			}
			if (null != (ICloudDriveService) mp.get("service")) {
				shareDriveService = (ICloudDriveService) mp.get("service");
			} else {
				PublicTools.restartApp(activity);
			}
		}
		super.onAttach(activity);
		LogUtil.i(LOG_TAG, "onAttach");
	}
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		LogUtil.i(LOG_TAG, "onCreate");
		// bindService
		super.onCreate(savedInstanceState);
	}
	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		LogUtil.i(LOG_TAG, "onDestroy");
	}
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		viewLayoutInflater = inflater.inflate(R.layout.fragment_upload_list, null);
		setBack(true);
		LogUtil.i(LOG_TAG, "onCreateView");
		findById();
		initData();
		return viewLayoutInflater;
	}
	private void findById(){
		upload_list_scrollview = (MyScrollView) viewLayoutInflater
				.findViewById(R.id.upload_list_scrollview);
		upload_list_layout = (LinearLayout) viewLayoutInflater
				.findViewById(R.id.upload_list_layout);
	}
	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
//		list = shareDriveService.getUploadFileList();
//		showMsgData(list);
	}
	/**
	 * 
	 * @param fileFolderList
	 */
	private void showMsgData(List<FileInfo> fileFolderList) {
		upload_list_scrollview.removeAllItem();
		if (null != fileFolderList && 0 < fileFolderList.size()) {
			BaseAdapter baseAdapter = new UploadListAdapter(activity,fileFolderList,shareDriveService,handler) ;
			upload_list_scrollview.setAdptor(baseAdapter);
			upload_list_scrollview.notifyAllItem();
		} else {
			upload_list_scrollview.resetList();
		}
	}
	private void initData(){
		upload_list_scrollview.setLayout(upload_list_layout);
	}

	@Override
	public void onDataLoaded() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void refreshListFromServer() {
		// TODO Auto-generated method stub
		
	}
	private Handler handler = new Handler(){
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case 1: 
				final View item = (View)msg.obj;
				list.remove(item);
				BaseAdapter baseAdapter = new UploadListAdapter(activity,list,shareDriveService,handler) ;
				upload_list_scrollview.setAdptor(baseAdapter);
				upload_list_scrollview.removeItem(item);
				break;
			case 2:
				break;
			}
		};
	};
}	

